/* Copyright 2009 Sony Mathew, Minneapolis MN. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package com.xsm.muck.gwt.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.xsm.gwt.widgets.client.AxisPanel;
import com.xsm.gwt.widgets.client.CustomPanel;
import com.xsm.lite.event.RemoteFailureEvent;
import com.xsm.lite.event.RemoteSessionTimeoutEvent;
import com.xsm.lite.util.StringUtil;
import com.xsm.muck.lite.event.OnyDoneEvent;
import com.xsm.muck.lite.event.RonyEvent;
import com.xsm.muck.lite.event.SonyEvent;
import com.xsm.muck.lite.event.TonyEvent;

/**
 * Listens to various events fired and prints messages associated with them.
 * For just general messages fire(new MsgEvent(txt));
 *  
 * See ChatPanel that fires these events.
 * 
 * @author Sony Mathew
 */
public class MsgPanel extends CustomPanel implements MsgEvent.Listener, SonyEvent.Listener, 
    RonyEvent.Listener, TonyEvent.Listener, OnyDoneEvent.Listener, RemoteFailureEvent.Listener, 
        RemoteSessionTimeoutEvent.Listener
{
    
    //my ioc dependencies
    public interface Context extends CustomPanel.Context {
    }
    
    private final Context cxt;
    private VerticalPanel msgContentPanel = null; 
    
    /**
     * author Sony Mathew
     */
    public MsgPanel(Context cxt) {
        super(cxt);
        this.cxt = cxt;
    }
    
    public MsgPanel build() {
        listen(MsgEvent.class);
        listen(SonyEvent.class);
        listen(RonyEvent.class);
        listen(TonyEvent.class);
        
        //remote reponses possibly coming back for above remote requests.
        listen(OnyDoneEvent.class);
        listen(RemoteFailureEvent.class);
        listen(RemoteSessionTimeoutEvent.class);

        
        Button clearBtn = new Button("Clear", new ClickHandler() {
            public void onClick(ClickEvent e) {
                msgContentPanel.clear();
            }            
        });
        
        //place holder for message content to go.
        msgContentPanel = new AxisPanel();
                        
        AxisPanel main = new AxisPanel().width("100%").style("borderWidth", "1px")
            .style("borderStyle", "solid").style("borderColor", "red")
            .x().space(3)
                .x("Messages").style("fontWeight","bold").q()
                .x(clearBtn).q()
                .q()
            .x(msgContentPanel).q();
        
        add(main);
        return this;
    }

    public void onEvent(MsgEvent e) {
        addMsg(e.getSrc(), e.getMsg());
    }

    public void onEvent(SonyEvent e) {
        addMsg("Sony", e.getMsg());
    }

    public void onEvent(RonyEvent e) {
        addMsg("Rony", e.getMsg());
    }

    public void onEvent(TonyEvent e) {
        addMsg("Tony", e.getMsg());
    }        
    
    public void onEvent(OnyDoneEvent e) {
        addMsg("Server", e.getResult());
    }            
    
    public void onEvent(RemoteFailureEvent e) {
        addMsg("Server", "Failed RPC of remote event [" + e.getRequest().getKey() + "]");
        addMsg("Server",  e.getRootCause());
    }            
    
    public void onEvent(RemoteSessionTimeoutEvent e) {
        addMsg("Server", "Session Timed out for RPC of remote event [" + e.getRequest().getKey() + "]");
    }            
    
    public void addMsg(String src, String msg) {
        msgContentPanel.add(new Label(src + ":" + "[" + msg + "]"));
    }
    
    public void addMsg(String src, Throwable t) {
        addMsg(src, StringUtil.clean(t.getMessage()));
        for(StackTraceElement se : t.getStackTrace()) {
            addMsg(src, StringUtil.clean(se.toString()));
        }
        if (t.getCause() != null) {
            addMsg(src, t.getCause());
        }
    }
    
}
